package com.demo.generate;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.DefaultJavaFormatter;
import org.mybatis.generator.api.dom.java.*;

import java.util.ArrayList;
import java.util.List;

/**
 * MyBatis Generator 增强插件，生成自定义的 Mapper 接口
 */
public class MapperInterfacePlugin extends PluginAdapter {

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        // 获取已有的 Mapper 接口
        FullyQualifiedJavaType baseMapperInterfaceType = new FullyQualifiedJavaType(introspectedTable.getMyBatis3JavaMapperType());
        Interface baseMapperInterface = new Interface(baseMapperInterfaceType);
        baseMapperInterface.setVisibility(JavaVisibility.PUBLIC);

        // 导入必要的类
        baseMapperInterface.addImportedType(new FullyQualifiedJavaType("java.util.List"));

        // 添加已有的方法
        for (Method method : baseMapperInterface.getMethods()) {
            baseMapperInterface.addMethod(method);
        }

        // 添加方法 getList
        int lastDotIndex = introspectedTable.getBaseRecordType().lastIndexOf(".");
        String type = introspectedTable.getBaseRecordType().substring(lastDotIndex + 1);
        addMethodIfNotExists(baseMapperInterface, "getList", new FullyQualifiedJavaType("List<" + type + ">"));

        // 添加方法 getOne
        addMethodIfNotExists(baseMapperInterface, "getOne", new FullyQualifiedJavaType(type));

        // 获取实体类的类型
        FullyQualifiedJavaType entityType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        // 添加泛型，使接口继承 BaseMapper<实体类类型>
        baseMapperInterface.addSuperInterface(new FullyQualifiedJavaType("BaseMapper<" + type + ">"));


        // 生成 Java 文件
        GeneratedJavaFile generatedJavaFile = new GeneratedJavaFile(baseMapperInterface, introspectedTable.getContext().getJavaClientGeneratorConfiguration().getTargetProject(), new DefaultJavaFormatter());

        // 返回生成的 Java 文件列表
        List<GeneratedJavaFile> generatedJavaFiles = new ArrayList<>();
        generatedJavaFiles.add(generatedJavaFile);

        return generatedJavaFiles;
    }


    private void addMethodIfNotExists(Interface baseMapperInterface, String methodName, FullyQualifiedJavaType returnType) {
        boolean methodExists = false;
        for (Method method : baseMapperInterface.getMethods()) {
            if (method.getName().equals(methodName)) {
                methodExists = true;
                break;
            }
        }

        if (!methodExists) {
            Method newMethod = new Method(methodName);
            newMethod.setVisibility(JavaVisibility.PUBLIC);
            newMethod.setReturnType(returnType);
            newMethod.setAbstract(true); // 设置为抽象方法
            baseMapperInterface.addMethod(newMethod);
        }
    }


}
